package com.example.jbservice.Controller;

import com.example.jbservice.Config.RedisConfiguration;
import com.example.jbservice.Dto.DeptDto;
import com.example.jbservice.Dto.PatientDto;
import com.example.jbservice.Dto.RegisterDTO;
import com.example.jbservice.Result.Result;
import com.example.jbservice.Service.RegisterService;
import com.example.jbservice.VO.PatientVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/register")
public class RegisterController {

    @Autowired
    private RegisterService registerService;

    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/save")
    public Result Save(@RequestBody RegisterDTO registerDTO) {

        log.info("开始111：{}",registerDTO.toString());
        registerDTO.setOper_date(LocalDateTime.now());
        registerDTO.setReg_date(LocalDateTime.now());
        registerService.Insert(registerDTO);
        return Result.success();
    }
    @GetMapping("/get")
    public String  Test()
    {
        log.info("查询科室字典");
       // String dept_name=registerService.Qry();
        String Key="dept";
        Map<String,String> redisMap=redisTemplate.opsForHash().entries(Key);
        if(redisMap!=null&&redisMap.size()>0)
        {
            return  redisMap.get("0003");
        }
       /* List<DeptDto> redislist = (List<DeptDto>) redisTemplate.opsForValue().get(Key);

        if(redislist!=null&&redislist.size()>0)
        {
            log.info("查询到数据:{}" ,redislist.size());
             return  redislist.get(0).getDept_name();
        }
        List<DeptDto> list=registerService.GetDept();
        redisTemplate.opsForValue().set(Key,list);*/
       /* Map<String,String> entries = redisTemplate.opsForHash().entries(Key);
        if(entries!=null&&entries.size()>0)
        {
            log.info("redis查询科室字典");
            return entries.get("0003");
        }
        Map<String,String> dept_map=registerService.SearchDept();
        log.info("数据库查询科室字典");
        if(dept_map!=null&&dept_map.size()>0)
        {
            log.info("查询到数据:{}" ,dept_map.size());
            for (String key : dept_map.keySet()) { //遍历key
                //System.out.println("key = " + key);
                log.info("查询到数据:{}" ,key);
            }
          //  log.info("查询到数据:{}" ,dept_map.size());
           // redisTemplate.opsForHash().putAll(Key,dept_map);
        }*/

        List<DeptDto> deptDtoList=registerService.GetDept();
       Map<String,String> dept_map= deptDtoList.stream().
               collect(Collectors.toMap(DeptDto::getDept_code,DeptDto::getDept_name));

        redisTemplate.opsForHash().putAll(Key,dept_map);
        return dept_map.get("0003");
    }

    @GetMapping ("/patient/{idenno}")
    public String GetPatientCardByIdenno(@PathVariable String idenno){

        log.info("患者身份证号为：{}",idenno);
        String card_no = registerService.GetPatientCardByIdenno(idenno);
        return   card_no;
    }
    @PostMapping("patient/register")
    public String CreatePatient(@RequestBody PatientDto patientDto)
    {
        log.info("患者基本信息为：{}",patientDto);
        registerService.CreatePatient(patientDto);
        return  patientDto.getCard_no();
    }


    @GetMapping ("/patientinfo/{cardno}")
    public Result<PatientDto> GetPatientInfoByCardNo(@PathVariable String cardno){
        log.info("患者身份卡号为：{}",cardno);
        PatientDto patientDto=registerService.GetPatientInfoByCardNo(cardno);
        return  Result.success(patientDto);
    }

    @PostMapping("/cancle")
    //@ApiOperation("订单搜索")
    public Result CancleReg(@RequestBody RegisterDTO registerDTO)
    {
        log.info("作废挂号入参：{}",registerDTO);
        registerService.cancleReg(registerDTO);
        return  Result.success();
    }


}
